#include "DemoApplyTorque.h"
#include"LSYEasyBody.h"
#include"LSYEasyWorld.h"
#include"Tool.h"

USING_NS_CC;

bool DemoApplyTorque::init()
{
	if (!AbstractBox2DTest::init(b2Vec2(0, -10)))
	{
		return false;
	}
	schedule(schedule_selector(DemoApplyTorque::work), 0.1, CC_REPEAT_FOREVER, 0.5);
	return true;
}

void DemoApplyTorque::box2DAppReady()
{
	auto size = Director::sharedDirector()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);
	//ground=LSYEasyBody::createBox(b2Vec2(size.width*0.5, 50), b2Vec2(size.width*0.5, 100));
	//ground->GetFixtureList()->SetFriction(1);

	boss = LSYEasyBody::createBox(b2Vec2(450,400), b2Vec2(50, 50));
}

void DemoApplyTorque::work(float dt)
{
	boss->SetAngularVelocity(-PI / 3);
	limitAngularVelocity(boss, ASPEED_LIMIT_BOSS);
}

void DemoApplyTorque::limitAngularVelocity(b2Body * body, float speedMax)
{
	float av = body->GetAngularVelocity();
	if (abs(av) > speedMax)
	{
		av = abs(av) / av * speedMax;
		body->SetAngularVelocity(av);
	}
}
